from fastapi import APIRouter, HTTPException, status, UploadFile
import string
import random
from fastapi.params import Depends
from utils.alysms import AliyunSMSSender
from services.address import AddressServiceClient
from utils.auth import AuthHandler
from schemas.request import CreateAddressModel, UpdateAddressModel, DeleteAddressModel
from schemas.response import AddressModel, ResultModel, UpdateModel, AddressesListModel

router = APIRouter(prefix="/address")
auth_handler = AuthHandler()
address_service_client = AddressServiceClient()

@router.post("/create", response_model=AddressModel)
async def create_address(data: CreateAddressModel, user_id: int=Depends(auth_handler.auth_access_dependency)):
    address = await address_service_client.create_address(
        user_id,
        realname=data.realname,
        mobile=data.mobile,
        region=data.region,
        detail=data.detail
    )
    return address

@router.put("/update", response_model=UpdateModel)
async def update_address(data: UpdateAddressModel, user_id: int=Depends(auth_handler.auth_access_dependency)):
    address = await address_service_client.update_address(
        id=data.id,
        realname=data.realname,
        mobile=data.mobile,
        region=data.region,
        detail=data.detail,
        user_id=user_id,
    )
    print(address)
    return UpdateModel()

@router.delete("/delete", response_model=UpdateModel)
async def delete_address(data: DeleteAddressModel, user_id: int=Depends(auth_handler.auth_access_dependency)):
    await address_service_client.delete_address(user_id=user_id, id=data.id)
    return UpdateModel()

@router.put("/detail/{id}", response_model=AddressModel)
async def get_address_by_id(id: str, user_id: int=Depends(auth_handler.auth_access_dependency)):
    address = await address_service_client.get_address_by_id(user_id=user_id, id=id)

    return address

@router.get("/list", response_model=AddressesListModel)
async def get_address_list(page: int=1, size: int=10, user_id: int=Depends(auth_handler.auth_access_dependency)):
    addresses = await address_service_client.get_address_list(user_id=user_id, page=page, size=size)

    return {"addresses": addresses}